const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    modal8: false,
    shop:null,
    car:null,
    carList:[],
    car_id:0,
    lf_tyre: false ,
    rf_tyre: false ,
    le_tyre: false ,
    re_tyre: false ,
    currentID: 0,
    isFirst:true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  init:function(){
    xrSdk.cookie.remove('tire_selection');
  },
  chooseShop:function(e){
    wx.navigateTo({
      url: "../../shop/search/search"
    })
  },
  initCache:function(){
    var tyre = xrSdk.cookie.get('tire_selection');
    this.setData({
      lf_tyre: tyre ? tyre.l_f_selected : false,
      rf_tyre: tyre ? tyre.r_f_selected : false,
      le_tyre: tyre ? tyre.l_e_selected : false,
      re_tyre: tyre ? tyre.r_e_selected : false,
    });
  },
  resetSelect:function(){
    this.setData({
      lf_tyre: false,
      rf_tyre: false,
      le_tyre: false,
      re_tyre: false
    });
  },
  selectRadio: function (e) {
    console.log(e);
    this.setData({
      car_id: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      car_id : xrSdk.cookie.get('car_id') || 0
    },()=>{
      that.initCarList();
      that.initCache();
    })
    app.checkMessageCount();
  },
  initCarList: function () {
    var that = this;
    var car_id = that.data.car_id;
    xrRequest.request(app,{
      url: config.service.get_car_list_for_install,
      login: true,
      data: {
        car_id: car_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          var id = res.data.data.car ? res.data.data.car.id : 0;
          if (that.data.car && id != that.data.car.id){
            that.resetSelect();
          }
          that.setData({
            carList: res.data.data.list,
            car: res.data.data.car,
            car_id: id
          });
          xrSdk.cookie.set('car_id', id);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  show8() {
    this.setData({
      modal8: true
    })
  },
  hide8() {
    this.setData({
      modal8: false
    })
  },
  handleClick(e) {
    var that = this;
    if (that.data.car_id == 0 || that.data.car_id < 1) {
      util.toast('请选择车辆');
      return;
    }
    this.hide8();
    this.initCarList();
  },
  gotoAddCar:function(e){
    wx.navigateTo({
      url: "../../car/add/add?source=1"
    });
    this.hide8();
  },
  /**
   * 下一步
   */
  nextClick:function(e){
    var that = this;
    if (that.data.car == null){
      util.toast('请选择车辆');
      return;
    }
    if (that.data.shop == null) {
      util.toast('请选择安装门店');
      return;
    }
    var lf_tyre = that.data.lf_tyre;
    var rf_tyre = that.data.rf_tyre;
    var le_tyre = that.data.le_tyre;
    var re_tyre = that.data.re_tyre;
    if (!lf_tyre && !rf_tyre && !le_tyre && !re_tyre){
      util.toast('请至少选择一条轮胎');
      return;
    }
    xrSdk.cookie.set('car_info', that.data.car);
    xrSdk.cookie.set('shop_info', that.data.shop);
    // 設定輪胎選擇cookie，記錄使用者選擇了哪個輪胎方位
    xrSdk.cookie.set('tire_selection', {
      'l_f_selected': lf_tyre,
      'r_f_selected': rf_tyre,
      'l_e_selected': le_tyre,
      'r_e_selected': re_tyre
    });
    // 清空下缓存
    that.resetTireInfoCookie();
    wx.navigateTo({
      url: "../input/input"
    })
  },
  checkboxChange:function(e){
    var that  = this;
    var type  = e.currentTarget.dataset.type;
    var check = e.detail.value.length > 0;
    var status = e.currentTarget.dataset.status;
    if (check && status == '1'){
      wx.showModal({
        title: '确认覆盖?',
        content: '此轮胎方位已注册轮胎，是否确定要更换新的轮胎？',
        success(res) {
          if (res.confirm) {
            console.log('用户点确定了')
          } else if (res.cancel) {
            console.log('用户点击取消');
            that.selectTyre(type, false);
          }
        }
      });  
    }
    that.selectTyre(type, check);
  },
  selectTyre: function (type, check){
    switch (type) {
      case 'lf':
        this.setData({
          lf_tyre: check
        });
        break;
      case 'rf':
        this.setData({
          rf_tyre: check
        });
        break;
      case 'le':
        this.setData({
          le_tyre: check
        });
        break;
      case 're':
        this.setData({
          re_tyre: check
        });
        break;
    }
    var lf_tyre = this.data.lf_tyre;
    var rf_tyre = this.data.rf_tyre;
    var le_tyre = this.data.le_tyre;
    var re_tyre = this.data.re_tyre;
    xrSdk.cookie.set('tire_selection', {
      'l_f_selected': lf_tyre,
      'r_f_selected': rf_tyre,
      'l_e_selected': le_tyre,
      'r_e_selected': re_tyre
    });
  },
  /**
   * 清空下缓存
   */
  resetTireInfoCookie:function () {
    xrSdk.cookie.remove('l_f_tire');
    xrSdk.cookie.remove('r_f_tire');
    xrSdk.cookie.remove('l_e_tire');
    xrSdk.cookie.remove('r_e_tire');
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})